// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Copyright (C) 2008 Vignette Corporation  All rights reserved.
// THIS PROGRAM IS CONFIDENTIAL AND AN UNPUBLISHED WORK AND TRADE SECRET
// OF THE COPYRIGHT HOLDER, AND DISTRIBUTED ONLY UNDER RESTRICTION.
// EXCEPT AS EXPLICITLY STATED IN A WRITTEN AGREEMENT BETWEEN THE PARTIES,
// THE SOFTWARE IS PROVIDED AS-IS, WITHOUT WARRANTIES OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
// NONINFRINGEMENT, PERFORMANCE, AND QUALITY.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import com.vignette.community.client.services.common.RemoteObjectEntity;
import com.vignette.community.client.services.common.XAPIConstants;
import com.vignette.community.client.services.dataservice.Action;
import com.vignette.community.client.services.dataservice.ActionData;
import com.vignette.community.client.services.dataservice.CommunityDataServiceFactory;
import com.vignette.community.client.services.dataservice.CommunityDataServiceIf;
import com.vignette.community.client.services.dataservice.CreateNewAction;
import com.vignette.community.client.services.dataservice.RemoteObjectActionData;
import com.vignette.community.client.services.dataservice.RemoteObjectQueryCreate;
import com.vignette.community.client.services.dataservice.Response;
import com.vignette.community.client.services.util.Util;

import javax.servlet.http.HttpServletResponse;


/**
 * Community Data example - Remote object creation. </br></br> For this example
 * to work the <code>vgncommsvcs.properties</code> should be found in the
 * classpath and should contain an entry based on the following: </br> <code>
 * CommunityServer1=collab,1,&lt;community_server_host&gt;,&lt;community_server_port&gt;,/xapi/,/xapi/,com.vignette.community.client.services.security.VssSecurityHandler,vssKey=changeme
 * </code>
 */
public class CDataCreateRemoteObject {

    public static void main(String[] args) {

        String communityServerId = "CommunityServer1";
        String currentUser = "admin";
        String password = null;

        CommunityDataServiceIf dservice =
                CommunityDataServiceFactory.getInstance()
                        .createCommunityDataService(communityServerId,
                                currentUser,
                                password);

        // Create a Remote Object
        String extobjid = "12345";
        String extobjcontext = "Sports Channel";
        String extsystemid = "1";
        String extsystemtype = "CMS";
        String extrealm = "News Site";
        String extobjtype = "Article";
        String otherval = XAPIConstants.EXT_OBJ_STATUS
                + "="
                + XAPIConstants.DEV_STATUS;

        RemoteObjectEntity ro = new RemoteObjectEntity(extobjid,
                extobjcontext, extsystemtype,
                extsystemid, extrealm, extobjtype,
                otherval);

        String ROName = "What is the most popular sport?";
        String RODescription = "Story comparing popular sports";

        Action createAction = new CreateNewAction();
        ActionData remoteActionData =
                new RemoteObjectActionData(ROName,
                        RODescription,
                        ro);
        RemoteObjectQueryCreate createQuery = new RemoteObjectQueryCreate(ro,
                createAction,
                remoteActionData, false);
        Response response1 = dservice.query(createQuery);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_MOVED_PERMANENTLY) {

            String newROOid = Util.getRedirectOid(response1);
            // Remote object has been created
        } else {
            // Remote object creation failed.
        }

    }
}
